// pages/addConfig/addConfig.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    configDataList: [{ index: 0, name: "床", check: false }, { index: 1, name: "电脑桌", check: false }, { index: 2, name: "餐桌", check: false }, { index: 3, name: "椅子", check: false }],
    hiddenAddConFModel: true,//隐藏配置弹窗
    addConfName: "",
    confLocal: [],//自定义添加的配置
    isShowDelete: false,//显示删除自定义配置
    manageValue: "管理",
  },
  //点击每项改变
  itemClick: function (e) {
    var idx = e.target.dataset.index;
    console.log(idx);
    this.data.configDataList[idx].check = !this.data.configDataList[idx].check;
    this.setData({
      configDataList: this.data.configDataList,
    })
  },
  //选择和删除自定义配置
  itemAddLocalClick: function (e) {
    var that = this;
    var idx = e.target.dataset.index;
    console.log(idx);
    if (this.data.isShowDelete) {
      wx.showModal({
        title: '删除提示',
        content: '是否要删除' + this.data.confLocal[idx].name,
        success: function (res) {
          if (res.confirm) {
            that.data.confLocal.splice(idx, 1)
            that.setData({
              confLocal: that.data.confLocal,
            })
            wx.setStorageSync('confLocal', that.data.confLocal)
          } else {

          }
        }
      })
    } else {
      this.data.confLocal[idx].check = !this.data.confLocal[idx].check;
      console.log(this.data.confLocal[idx])
      this.setData({
        confLocal: this.data.confLocal,
      })
    }


  },
  //确认添加配置点击
  addConfigBnt: function () {
    console.log(55)
    var datas = [];
    for (var i = 0; i < this.data.configDataList.length; i++) {
      if (this.data.configDataList[i].check) {
        datas.push(this.data.configDataList[i]);
      }
    }
    for (var i = 0; i < this.data.confLocal.length; i++) {
      if (this.data.confLocal[i].check) {
        datas.push(this.data.confLocal[i]);
      }
    }
    wx.setStorageSync('addconfig', datas)
    wx.setStorageSync('configList', datas)
    wx.navigateBack();
  },
  itemAddClick: function () {
    var datas = [];
    for (var i = 0; i < this.data.configDataList.length; i++) {
      if (this.data.configDataList[i].check) {
        datas.push(this.data.configDataList[i]);
      }
    }
    wx.setStorageSync('addconfig', datas)
    wx.navigateBack();
  },

  //添加自定义配
  addConFClick: function () {
    this.setData({
      hiddenAddConFModel: false
    })

  },
  //关闭
  closeModelView: function () {
    this.setData({
      hiddenAddConFModel: true,//隐藏配置弹窗
      addConfName: "",
    })
  },
  //管理点击
  manageClick: function () {
    var name = !this.data.isShowDelete ? '取消' : '管理';
    var b = !this.data.isShowDelete;
    this.setData({
      isShowDelete: b,
      manageValue: name
    });
  },
  // 确认
  confirm: function () {
    var obj = { index: 1, name: this.data.addConfName, check: false, local: 1 }
    if (obj) {
      var s = this.data.confLocal.concat(obj);
      this.setData({
        confLocal: s,
      })
      wx.setStorageSync('confLocal', this.data.confLocal)
    }
    this.closeModelView();
  },
  //输入框
  addConfInput: function (e) {
    this.setData({
      addConfName: e.detail.value,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var confL = wx.getStorageSync("confLocal")
    for (var i in confL) {
      confL[i].check=false;
    }
    if (confL) {
      this.setData({
        confLocal: confL,
      })
    }
    //获取页面传送过来的配置
    var cDataList = wx.getStorageSync("conf");
    wx.removeStorageSync('conf')
    var configList = wx.getStorageSync('configList')
    if (cDataList) {
      this.initConfData(cDataList);
    } else if (configList){
      this.initConfData(configList);
    }
  },
  
  initConfData:function(datas) {
    console.log('00000')
    console.log(datas)
    for (var i = 0; i < datas.length; i++) {
      console.log('1111')
      // if (datas[i].local) {
        for (var j in this.data.confLocal) {
          console.log('22222')
          console.log(datas[i])
          if (datas[i].name == this.data.confLocal[j].name) {
            console.log('3333')
            console.log(datas[i])
            this.data.confLocal[j].check = datas[i].check
          }
        }
      // } else {
        for (var k in this.data.configDataList) {
          if (datas[i].name == this.data.configDataList[k].name) {
            this.data.configDataList[k].check = datas[i].check
          }
        }
      // }
    }
    this.setData({
      confLocal: this.data.confLocal,
      configDataList: this.data.configDataList
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})